﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente
{
    public class Animation
    {

        public static event EventHandler onAnimationcompleted;

        // FADE IN
        public static void FadeIn(UIElement elem, double duration)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(duration));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, elem);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            //sb.Completed += new EventHandler(sb_Completed);
            sb.Begin();
        }

        static void sb_Completed(object sender, EventArgs e)
        {
            if (onAnimationcompleted != null)
                onAnimationcompleted(sender, e);
        }

        // FADE OUT
        /// <summary>
        /// Animazione che fa scomparire gradualmente un elemento della scena
        /// </summary>
        /// <param name="elem">elemnto su cui applicare animazione</param>
        /// <param name="duration">durata in millisecondi / 1 sec = 1000 </param>
        public static void FadeOut(UIElement elem, double duration)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 1;
            doubleAnimation.To = 0;
            doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(duration));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, elem);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            //sb.Completed += new EventHandler(sb_ShowCompleted);
            sb.Begin();
        }

        public static void Move(UIElement elem, int FromX, int FromY, int ToX, int ToY, double sec)
        {
            EasingFunctionBase[] ef = new EasingFunctionBase[]{
            new BackEase(),
            new BounceEase(),
            new CircleEase(),
            new CubicEase(),
            new ElasticEase(),
            new ExponentialEase(),
            new PowerEase(),
            new QuadraticEase(),
            new QuarticEase(),
            new QuinticEase(),
            new SineEase()
            };

            int eFindex = new Random().Next(0, ef.Length);
            int eFindex2 = new Random().Next(0, ef.Length);

            // ef[eFindex].EasingMode = EasingMode.EaseIn;
            // ef[eFindex].EasingMode = EasingMode.EaseOut;
            ef[eFindex].EasingMode = EasingMode.EaseInOut;

            DoubleAnimation doubleAnimationY = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromMilliseconds(sec)),
                //EasingFunction = ef[eFindex]
            };

            DoubleAnimation doubleAnimationX = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromMilliseconds(sec)),
                //EasingFunction = ef[eFindex2]
            };

            Storyboard sb = new Storyboard();

            elem.RenderTransform = new TranslateTransform();

            doubleAnimationY.From = FromY;
            doubleAnimationY.To = ToY;

            doubleAnimationX.From = FromX;
            doubleAnimationX.To = ToX;

            Storyboard.SetTargetProperty(doubleAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
            Storyboard.SetTargetProperty(doubleAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));

            Storyboard.SetTarget(doubleAnimationY, elem);
            Storyboard.SetTarget(doubleAnimationX, elem);

            sb.Children.Add(doubleAnimationY);
            sb.Children.Add(doubleAnimationX);

            //sb.Completed += new EventHandler(sb_MoveCompleted);
            sb.Begin();
        }
    }
}
